package graph.day16;

import java.util.*;

public class SPFA {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] minDist = new int[n + 1];
        Arrays.fill(minDist, Integer.MAX_VALUE);
        minDist[1] = 0;
        List<List<int[]>> edges = new ArrayList<>();
        for (int i = 0; i <= n; i++) edges.add(new ArrayList<>());
        while (m-- > 0) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int w = sc.nextInt();
            edges.get(a).add(new int[]{b, w});
        }
        Queue<Integer> queue = new LinkedList<>();// 用于存放当前访问节点的临边节点
        queue.offer(1);
        while (!queue.isEmpty()) {
            int node = queue.poll();
            for (int[] ne : edges.get(node)) {
                int to = ne[0];
                int w = ne[1];
                if (minDist[to] > minDist[node] + w) {
                    minDist[to] = minDist[node] + w;
                    queue.offer(to);
                }
            }
        }
        if (minDist[n] == Integer.MAX_VALUE) System.out.println("unconnected");
        else System.out.println(minDist[n]);
    }
}
